#include "include/MainWindow.h"
#include "include/SecureTestWindow.h"
#include "include/CertTestWindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget* pwgt)
    :QMainWindow(pwgt), embPixmap(":/img/emb.png"), inWind(NULL)
{
    createEmbWidget();
    createActions();
    createMenu();
    createStatusBar();
}

MainWindow::~MainWindow()
{
}

void MainWindow::createEmbWidget()
{
    ql = new QLabel("D");
    ql->setPixmap(embPixmap);
    setCentralWidget(ql);
}

void MainWindow::createActions()
{
    rawTestAct = new QAction("&Raw Test", NULL);
    connect(rawTestAct, SIGNAL(triggered()), this, SLOT(rawTest()));

    secureTestAct = new QAction("&Secure Test", NULL);
    connect(secureTestAct, SIGNAL(triggered()), this, SLOT(secureTest()));

    clearAct = new QAction("&Clear", NULL);
    connect(clearAct, SIGNAL(triggered()), this, SLOT(clearMainForm()));

    certTestAct = new QAction("C&ert Test", NULL);
    connect(certTestAct, SIGNAL(triggered()), this, SLOT(certTest()));

    optionsAct = new QAction("&Options", NULL);
    connect(optionsAct, SIGNAL(triggered()), this, SLOT(options()));

    exitAct = new QAction("&Exit", NULL);
    connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
}

void MainWindow::createMenu()
{
    testMenu = this->menuBar()->addMenu("&Test");
    testMenu->addAction(certTestAct);
    testMenu->addAction(secureTestAct);
    testMenu->addAction(rawTestAct);
    testMenu->addAction(clearAct);

    this->menuBar()->addAction(optionsAct);
    this->menuBar()->addAction(exitAct);
}

void MainWindow::createStatusBar()
{
    statusBarLable = new QLabel("Status:OK");
    statusBar()->addWidget(statusBarLable);
}

void MainWindow::secureTest()
{
    if(!inWind || (inWind->objectName() != "secure"))
    {
        inWind = new SecureTestWindow;
        inWind->resize(embPixmap.size());
        setCentralWidget(inWind);
    }
}

void MainWindow::rawTest()
{
}

void MainWindow::clearMainForm()
{
    if(inWind)
    {
        inWind->clearInnerWindow();
        inWind = NULL;
    }

    createEmbWidget();
    setCentralWidget(ql);
}

void MainWindow::certTest()
{
    if(!inWind || (inWind->objectName() != "cert"))
    {
        inWind = new CertTestWindow;
        inWind->resize(embPixmap.size());
        setCentralWidget(inWind);
    }
}

void MainWindow::options()
{
}
